package com.ruoyi.web.controller.demo.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeMergePrecreateRequest;
import com.alipay.api.response.AlipayTradeMergePrecreateResponse;

public class AlipayTradeMergePrecreate {
    public static void main(String[] args) throws AlipayApiException {
        String appCertPath = "/Users/mac/Documents/WORK/场景/直付通11/appCertPublicKey_2021003196668931.crt";
        // 支付宝公钥证书路径
        String alipayCertPath = "/Users/mac/Documents/WORK/场景/直付通11/alipayCertPublicKey_RSA2.crt";
        // 支付宝根证书路径
        String alipayRootCertPath = "/Users/mac/Documents/WORK/场景/直付通11/alipayRootCert.crt";

        // 支付宝网关
        String serverUrl = "https://openapi.alipay.com/gateway.do";
        // 应用APPID
        String appId = "2021003196668931";
        // 初始化SDK
        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
        //设置网关地址
        certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
        //设置应用Id
        certAlipayRequest.setAppId(appId);
        //设置应用私钥
        certAlipayRequest.setPrivateKey("MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCYjCsmZsld84YkTStL/AATrGzCmSLb6J4FZGV4eYSZWdGhsPDf3XEzCMTL/g3WvDNIISGiVek2NYXGKqy6VMdKHoS+miypmJVZ3EkgPwj/0hJWeyrKvWFn1nIUeW8u7mnJ7KJl51Opaj/+RUtkGWHKxOrBCY5KT2q7hiUTszuLCt23muU8dF9nxL9Kc+x6NgtPhj/oGdJNgQZMdjBqvshZ06oVoHcWDzK+zJJmft4YI+Km1+Uvcyk5ZtPDFXaqeFuSCUPBdRHq9AdoUG/CW1Xy0xVcICAZnI0SzuhKH9eEhyuLQ8yKHp8kajzXTxCUgKU5qI8wQmI2YiupVdQ+HqhDAgMBAAECggEAUcxnnvHi7HQW7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ERyViUBPTtLD+96HpLvNgiCtGTi+N0i0xa3mxyrOJwIvnKawCrHjO44wrtW59XLD6Ns8J2VxW9BYuGNFJ7SU+PXJ5nVNhdC9USLs9B7agLOJ9mOmNWCRuW1Jvrs=");
        //设置请求格式，固定值json
        certAlipayRequest.setFormat("json");
        //设置字符集
        certAlipayRequest.setCharset("UTF-8");
        //设置签名类型
        certAlipayRequest.setSignType("RSA2");
        //设置应用公钥证书路径
        certAlipayRequest.setCertPath(appCertPath);
        //设置支付宝公钥证书路径
        certAlipayRequest.setAlipayPublicCertPath(alipayCertPath);
        //设置支付宝根证书路径
        certAlipayRequest.setRootCertPath(alipayRootCertPath);
        //构造client
        AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
        AlipayTradeMergePrecreateRequest request = new AlipayTradeMergePrecreateRequest();
        request.setNotifyUrl("http://1r71ij4lzb9k.ngrok.xiaomiqiu123.top/");
        request.setReturnUrl("");
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_merge_no", "MERGE20240101123456789");
        bizContent.put("timeout_express", "10m");

        JSONArray orderDetails = new JSONArray();

        JSONObject order1 = new JSONObject();
        order1.put("app_id", "2021003196668931");
        order1.put("out_trade_no", "20240817010101005");
        order1.put("subject", "测试商品1");
        order1.put("total_amount", 0.01);
        order1.put("product_code", "QUICK_WAP_WAY");
        // 结算信息，按需传入
        JSONObject settleInfo1 = new JSONObject();
        JSONArray settleDetailInfos1 = new JSONArray();
        JSONObject settleDetail1 = new JSONObject();
        settleDetail1.put("trans_in_type", "defaultSettle");
        settleDetail1.put("amount", 0.01);
        settleDetailInfos1.add(settleDetail1);
        settleInfo1.put("settle_detail_infos", settleDetailInfos1);
        order1.put("settle_info", settleInfo1);
        // 二级商户信息，按需传入
        JSONObject subMerchant1 = new JSONObject();
        subMerchant1.put("merchant_id", "2088240169943631");
        order1.put("sub_merchant", subMerchant1);
        orderDetails.add(order1);

        JSONObject order2 = new JSONObject();
        order2.put("app_id", "2021003196668931");
        order2.put("out_trade_no", "20240817010101006");
        order2.put("subject", "测试商品2");
        order2.put("total_amount", 0.01);
        order2.put("product_code", "QUICK_WAP_WAY");
        // 结算信息，按需传入
        JSONObject settleInfo2 = new JSONObject();
        JSONArray settleDetailInfos2 = new JSONArray();
        JSONObject settleDetail2 = new JSONObject();
        settleDetail2.put("trans_in_type", "defaultSettle");
        settleDetail2.put("amount", 0.01);
        settleDetailInfos2.add(settleDetail2);
        settleInfo2.put("settle_detail_infos", settleDetailInfos2);
        order2.put("settle_info", settleInfo2);
        // 二级商户信息，按需传入
        JSONObject subMerchant2 = new JSONObject();
        subMerchant2.put("merchant_id", "2088240169943631");
        order2.put("sub_merchant", subMerchant2);
        orderDetails.add(order2);

        bizContent.put("order_details", orderDetails);

        request.setBizContent(bizContent.toString());
        AlipayTradeMergePrecreateResponse response = alipayClient.certificateExecute(request);
        if(response.isSuccess()){
            System.out.println("调用成功"+JSONObject.toJSON(response));
        } else {
            System.out.println("调用失败"+JSONObject.toJSON(response));
        }
    }
}
